/*********************************************************************
/*File: 		main.c
/*Date: 		June 18, 2010
/*Description: 	Contains main function and support functions for 
/*				initializing the PIC16F1938 and managing the timer.
/*				 
/*			
/*Author: 		Jason Reber
/*********************************************************************/


#include <pic.h>
#include <stdlib.h>
#include <time.h>
#include "mmLCD.c"
#include "mmAtoD.c"

/*****************************************************************************/
/*	DEFINTIONS
/*****************************************************************************/
//#define ZERO_OFFSET 351		//same unipolar offset used by Mentor.asm
#define ZERO_OFFSET 351
#define DEADBAND 2			//crossover distortion in sensor around zero pressure point
#define AUTOZERO 5			//same principal as Mentor.asm. If Mentor is initialized 
							//"open to air" (zero pressure), the ZERO_OFFSET value will
							//be set to the measured value read at power-up.
							
//#define FOSC                     32000000UL
// Timer0: 1:32 prescale
//#define OPTION_REG_INIT          0b10000101
//#define TIMER0_PRESCALE          64L

//__CONFIG(0x31C4);   //no monitors active, no code prot, internal osc.
__CONFIG(0x3FE4);
__CONFIG(0x1EFF);	//


/*****************************************************************************/
/*	FUNCTION PROTOTYPES
/*****************************************************************************/
void initPorts(void);

/*****************************************************************************/
/* LOCAL VARIABLES
/*****************************************************************************/

mmDisp MentorData;    //this struct represents the 3 digits and the symbols on 
                      //the Mentor LCD Display
bit fAutoZeroComplete;  //flag to indicate that AutoZero has been conducted.
					

/****************************************************************************
  Function:  	  int main()
  Summary:        Calls functions to initialize PIC, then starts timer. All 
  				  further processor functionality will be called from timer
  				  ISR.
  Precondition:   None
  Parameters:     None
  Returns:		  None
  ***************************************************************************/
int main(void)
{
  initPorts();
  mmLCDInit();	
  mmInitAtoD();
  fAutoZeroComplete = FALSE;
unsigned int i, j, SensorVal, Offset;
	i=0;
	j= 0;
	
mmLCDClear(MentorData);

/*  The following if statement is executed only at startup. If measured 
    pressure falls in |AUTOZERO|, Offset will be adjusted to this measured 
	value. Otherwise, default ZERO_OFFSET value will be used. 
*/


 if(!fAutoZeroComplete)		
  {
    SensorVal = mmFilterAtoD();  //Get averaged 16 reads from pressure sensor.
    if((abs(SensorVal)- ZERO_OFFSET)<=5)
      Offset = SensorVal;
      else
        Offset = ZERO_OFFSET;
    fAutoZeroComplete = TRUE;
  }

LCDEN = 1;		//turn on the LCD
while(1){	
	for(i=0;i<0xF000;i++);   //delay
	RC4 = 0;	//clear for now to save power
	SensorVal = mmFilterAtoD();	//Get the filtered value at AN0.

	if((SensorVal >(ZERO_OFFSET-2))&&(SensorVal<ZERO_OFFSET+2))  //If in dead band, do another read.
	     SensorVal = mmFilterAtoD();

	{
		if(SensorVal>= Offset) 
		  {
		    SensorVal -= Offset;
		    MentorData.dispsym &= ~MINUS_;   //turn minus symbol off.
		  }
		  else
		    {
			    SensorVal = Offset - SensorVal;
			    MentorData.dispsym |= MINUS_;  //turn minus symbol on.
			} 
	    if(SensorVal< DEADBAND)
	      SensorVal = 0;
	}
	      
 	if(SensorVal>305)
 	  mmDisplayOpr();   //show "OPr" (over pressure) on display.
 	  else
 	{
	 	MentorData.digit2 = SensorVal %10;
	    SensorVal /= 10;
	    MentorData.digit1 = SensorVal % 10;
	    SensorVal /= 10;
	    MentorData.digit0 = SensorVal % 10;
		mmDisplayDigits(MentorData);   //Update the display
     }
   }     
}  


/****************************************************************************
  Function:  	  void initPorts()
  Summary:        Initializes the I/O ports on the PIC16F1938.Configures
  				  internal osc frequency, configures I/O for Mentor.
  				  application.
  Precondition:   None
  Parameters:     None
  Returns:		  None
  ***************************************************************************/
void initPorts()
{
  	OSCCON |= 0x68;	//set internal osc to 4 MHz.
	ANSELA = 0x01;   //all DIO except A1.
  	TRISA = 0x01;	//all outputs except A1.
  	TRISC = 0x0;	//All port C outputs.
  	ANSELB = 0x00;   //all DIO
  	TRISB = 0x02;   //PB1 is input
  	WPUB1 = 0x1;	//enable weak pull up.
  	//Now set up the FVR as the V+ source for the AtoD.
  	ADFVR0 = 1;
  	ADFVR1 = 1;    //Configure for 4.096V FVR
  	FVREN = 1;		//Enable the FVR
}


